package com.cskaoyan.service.wxService;

import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.CommentExample;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.vo.CommentList1Vo;
import com.cskaoyan.bean.vo.CommentList2Vo;
import com.cskaoyan.bean.vo.CommentList3Vo;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public Map<String, Object> getCommentCount(Integer valueId, Byte type) {
        CommentExample commentExample = new CommentExample();
        CommentExample commentExample1 = new CommentExample();
        commentExample.createCriteria().andTypeEqualTo(type).andValueIdEqualTo(valueId);
        commentExample1.createCriteria().andValueIdEqualTo(valueId).andTypeEqualTo(type).andHasPictureEqualTo(true);
        List<Comment> comments = commentMapper.selectByExample(commentExample1);
        List<Comment> comments1 = commentMapper.selectByExample(commentExample);
        int commentsWithPic = comments.size();
        int allComments = comments1.size();
        Map<String,Object> countComment = new HashMap<>();
        countComment.put("hasPicCount",commentsWithPic);
        countComment.put("allCount",allComments);
        return countComment;
    }

    @Override
    public CommentList1Vo getCommentList(Integer valueId, Byte type, Integer size, Integer page, Integer showType) {
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andValueIdEqualTo(valueId).andTypeEqualTo(type);
        if (showType == 1){
            commentExample.createCriteria().andHasPictureEqualTo(true);
        }
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        List<CommentList2Vo> list = new LinkedList<>();
        CommentList1Vo commentList1Vo = new CommentList1Vo();
        for (Comment com: comments) {
            User user = userMapper.selectByPrimaryKey(com.getUserId());
            CommentList3Vo commentList3Vo = new CommentList3Vo();
            commentList3Vo.setAvatarUrl(user.getAvatar());
            commentList3Vo.setNickName(user.getNickname());

            CommentList2Vo commentList2Vo = new CommentList2Vo();
            commentList2Vo.setAddTime(com.getAddTime());
            commentList2Vo.setContent(com.getContent());
            commentList2Vo.setPicList(com.getPicUrls());
            commentList2Vo.setUserInfo(commentList3Vo);

            list.add(commentList2Vo);
        }
        PageHelper.startPage(page,size);
        commentList1Vo.setData(list);
        PageInfo pageInfo = new PageInfo(commentList1Vo.getData());
        Integer totalPage = new Integer((int) pageInfo.getTotal());
        Integer pageNumber = new Integer(pageInfo.getPageNum());
        commentList1Vo.setCount(totalPage);
        commentList1Vo.setCurrentPage(pageNumber);
        return commentList1Vo;
    }

    @Override
    public Comment postComment(Byte type, Integer valueId, String content,
                               Short star, Boolean hasPicture,String[] picUrls) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();

        Comment comment = new Comment();
        comment.setUserId(user.getId());
        comment.setValueId(valueId);
        comment.setType(type);
        comment.setContent(content);
        comment.setUserId(user.getId());
        comment.setHasPicture(hasPicture);
        comment.setStar(star);
        comment.setAddTime(new Date());
        comment.setUpdateTime(new Date());
        comment.setDeleted(false);
        comment.setPicUrls(picUrls);
        comment.setDeleted(false);
        commentMapper.insertSelective(comment);
        return comment;
    }

    @Override
    public Integer selectGoodsId(Integer orderGoodsId) {
        Integer goodsId = commentMapper.selectGoodsId(orderGoodsId);
        return goodsId;
    }
}